package com.tansun.easycare.rule.datacomponent.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.tansun.easycare.core.utils.ResponseTable;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.common.model.ResponseVO;
import com.tansun.easycare.rule.common.web.model.ProjectInfo;
import com.tansun.easycare.rule.datacomponent.model.DataMiningVo;
import com.tansun.easycare.rule.datacomponent.service.IDataMiningService;
import com.tansun.easycare.rule.rulemange.model.RuleManageVO;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.rule.common.RuleModelTypeEnum;
import com.tansun.rule.utils.LoggerManager;

@RestController
@RequestMapping(value = "/ruleProj/datamining")
public class DataMiningController extends RuleBaseController  {

	 @Autowired
	 private RuleManageService ruleManageService;
	 
	 @Autowired
	 private IDataMiningService dataMiningService;
	 
	/**
     * 跳转数据挖掘表列表
     */
    @RequestMapping("index")
    public ResponseTable<RuleManageVO> index(@RequestBody RuleManageVO param) throws Exception {
    	param.setModelType(RuleModelTypeEnum.DataMining.getCode());
    	PageInfo<RuleManageVO> pageInfo = this.ruleManageService.findRuleManageVO(param.getPage(), param);
        ResponseTable<RuleManageVO>  resVo  = new  ResponseTable<RuleManageVO>(pageInfo);
        return resVo;
    }
    
    @RequestMapping("upload")
    public ResponseVO importPMML(@RequestParam("file") MultipartFile file,HttpServletRequest request, HttpServletResponse res,String projectCode,String optType,String rankRelationId,String userId) throws Exception {
    	ResponseVO msg = new ResponseVO();
        try {
        	dataMiningService.upload(file,projectCode,optType,rankRelationId,userId);
        } catch (Exception e) {
        	LoggerManager.error(e.getMessage(), e, this.getClass());
        	msg.setStatus(ResponseVO.ERROR);
            msg.setDescription(e.getMessage());
        }
        return  msg;
    }
    
    /**
     * 跳转数据挖掘列表列表
     * @param code
     * @param projectCode
     * @param rankRelationId
     * @return
     * @throws Exception
     */
    @RequestMapping("detail")
    public ResponseVO detail(@RequestBody ProjectInfo projectInfo) throws Exception {
    	ResponseVO res = new ResponseVO();
    	res.setStatus(ResponseVO.OK);
    	DataMiningVo vo = dataMiningService.findDataMiningModdel(projectInfo.getCode(), projectInfo.getProjectCode());
        res.setData(vo);
        return  res;
    }
    
    /**
     * 跳转数据挖掘列表列表
     */
    @RequestMapping("save")
    @ResponseBody
    public ResponseVO save(@RequestBody DataMiningVo vo){
    	ResponseVO ret = new ResponseVO();
    	try {
			dataMiningService.saveOrUpdate(vo);
		} catch (Exception e) {
			LoggerManager.error(e.getMessage(), e, this.getClass());
            ret.setDescription(e.getMessage());
            ret.setStatus(ResponseVO.ERROR);
		}
        return ret;
    }
}
